﻿using Cl.Model.Results;
using Cl.Utils;
using Cl.Utils.Signalr.Hub.HubRegister;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Service.SignalR.Sservice
{
    public class SignalrService : ISignalrService
    {
        private IHubContext<GlobalHub> GlobalHubContext;

        public SignalrService(IHubContext<GlobalHub> globalHubContext)
        {
            GlobalHubContext = globalHubContext;
        }

        /// <summary>
        /// 通知刷新统计信息
        /// </summary>
        public async Task NotifyRefreshChartInfo()
        {
            foreach (var connected in GlobalService.SignalrConnectedLists)
            {
                await GlobalHubContext.Clients.Client(connected.ConnectionId).SendAsync("NotifyRefreshChartInfo", connected, true);
            }
        }

        #region 公用方法

        public ResultInfoModel GetAllConnSignalrList()
        {
            var obj = GlobalService.SignalrConnectedLists;
            return GetResult.Success("", obj);
        }

        public async Task NotifyTest()
        {
            await GlobalHubContext.Clients.All.SendAsync("NotifyTest", true, "testData");
        }
        #endregion
    }
}
